package com.seata.demo.seatarole.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.seata.demo.seatarole.entity.Role;
import com.seata.demo.seatarole.feign.UserFeign;
import com.seata.demo.seatarole.handel.ControllerFallbackHandel;
import com.seata.demo.seatarole.handel.ControllerHandel;
import com.seata.demo.seatarole.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    UserFeign userFeign;

    @PostMapping("/role/add")
    public void add(@RequestBody Role role){
        roleService.insert(role);
    }

    @GetMapping("/test")
    public void test(){
        System.out.println("test");
    }

    @GetMapping("/test2")
    public void test2() {
        try {
           Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("test2");
    }

    @GetMapping("/testKey")
    @SentinelResource(value = "testKey")
    public String testKey(@RequestParam(value = "p1",required = false) String p1,@RequestParam(value = "p2",required = false) String p2){
        return "testKey";
    }

    @GetMapping("/testHandel")
    @SentinelResource(value = "testHandel",blockHandlerClass = ControllerHandel.class,blockHandler = "handelException",
    fallback = "fallbackTest",fallbackClass = ControllerFallbackHandel.class,exceptionsToIgnore = {IllegalAccessException.class})
    public String testHandel(@RequestParam("id") String id){
        switch (id){
            case "1":{
                return "成功";
            }
            case "2": {
                return "no yes";
            }
            case "3":{
                int a=1/0;
            }
            case "4":{
                throw new RuntimeException("非法参数异常");
            }
        }
        return "正常输出";
    }

    @GetMapping("/ttt")
    public String ttt(@RequestParam("id") String id){
        return userFeign.testFeign(id);
    }

//    public String fallbackTest(@RequestParam("id") String id,Throwable e){
//        return "fallback捕获";
//    }



}

